﻿using GCC.APIContainer.Utilities;
using GCC.Container.Abstracts;
using GCC.Container.Models;

using GCCGrpc.FileServer;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Z.Test.BlendMode
{
    [ApiController]
    [Route("TestServer/[controller]")]
    public class APIServer : GCCAPIControllerBase
    {
        public APIServer(AbsGCCContext context) : base(context)
        {
        }

        /// <summary>
        /// 我是xxxx
        /// </summary>
        /// <returns></returns>
        [HttpGet()]
        public DataResponse<string> Get()
        {
            return HandleData(Guid.NewGuid().ToString("N"));

        }

        [HttpPost("setFile")]
        public DataResponse<GRPCCreateBigFileResponse> SetFile(IFormFile file)
        {
            GCCContext.SaveTrace("custome", "诶~我存了一下,", path: "controller's method", method: nameof(SetFile));
            if (file is null)
            {
                return InvalidParameter<GRPCCreateBigFileResponse>();
            }
            var fileServerHost = GCCContext.GetConfigByPath(new string[] { GCCContext.PointName ?? "", "FileServer" });
            var fileFolderPath = Path.Combine(AppContext.BaseDirectory, Guid.NewGuid().ToString("N"));
            var filePath = Path.Combine(fileFolderPath, file.FileName);
            _ = Directory.CreateDirectory(fileFolderPath);
            {
                using var fs = System.IO.File.Open(filePath, FileMode.CreateNew, FileAccess.ReadWrite);
                file.CopyTo(fs);
                fs.Flush();
                fs.Close();
            }
            GCCContext.SaveTrace("custome", "诶~我又存了一下,");
            GRPCCreateBigFileResponse? res = null;
            foreach (var item in System.FileServer.Client.Manager.GRPCCreateBigFile(fileServerHost, filePath, GCCContext.EventID ?? ""))
            {
                res = item.res;
            }


            if (res is null)
            {
                GCCContext.SaveTrace("custome", "诶~错误返回,");
                return InvalidParameter<GRPCCreateBigFileResponse>();
            }
            GCCContext.SaveTrace("custome", "诶~正确返回,");
            return HandleData(res);
        }
    }
}